package com.zhf.demo02;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/12 22:57
 */
public class ParameterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置接收浏览器信息编码方式
        request.setCharacterEncoding("utf-8");
        //获取指定参数名称对应的参数值并打印
        System.out.println("获取到的姓名为："+request.getParameter("name"));
        System.out.println("获取到的爱好有：");
        String[] hobbies = request.getParameterValues("hobby");
        for (String s:
             hobbies) {
            System.out.println(s);
        }
        System.out.println("----------------------");
        //获取所有参数名称
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            System.out.println(parameterNames.nextElement());
        }
        System.out.println("----------------------");
        //获取请求参数名和对应值的第二种方式
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将map集合中所有键值对组成Set集合
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        //遍历set集合
        for (Map.Entry<String, String[]> key:
             entries) {
            System.out.println(key.getKey()+":");
            for (String s:
                 key.getValue()) {
                System.out.print(s+" ");
            }
        }
        System.out.println("----------------------");
        //获取客户端请求的其他信息
        System.out.println("发送请求的客户端ip地址为："+request.getRemoteAddr());
        System.out.println("发送请求的客户端端口号为："+request.getRemotePort());
        System.out.println("请求资源的路径为："+request.getRequestURI());
        System.out.println("轻松资源的完整路径为："+request.getRequestURL());
        System.out.println("请求方式为："+request.getMethod());
        System.out.println("请求附带的参数为："+request.getQueryString());
        System.out.println("请求的Servlet路径为："+request.getServletPath());
        System.out.println("----------------------");
        //向浏览器发送相应数据
        //获取响应数据的默认编码方式
        String characterEncoding = response.getCharacterEncoding();
        System.out.println("响应编码方式为:"+characterEncoding);
        //设置服务器和浏览器的编码方式以及文本类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
//        writer.write("I received! 我收到了！");
        Random random = new Random();
        int i = random.nextInt(100) + 1;
        writer.write("<h1>"+i+"<h1/>");
        System.out.println("相应浏览器成功");
        writer.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
